package top.baselitch.sample.dbutils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import top.baselitch.sample.beans.User;

public class UserDaoImpl implements UserDao {
    private DbHelper dbHelper;
    public UserDaoImpl(Context context){
        dbHelper = new DbHelper(context);
    }
    @Override
    public void addUser(User user) {
        SQLiteDatabase sd = dbHelper.getWritableDatabase();
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append("INSERT INTO ")
                .append(DbHelper.TABLE_NAME)
                .append(" (")
                .append("userName,email,passwd")
                .append(") VALUES(")
                .append("'" + user.getUserName() + "'")
                .append(",")
                .append("'" + user.getEmail() + "'")
                .append(",")
                .append("'" + user.getPasswd() + "'")
                .append(")");
        sd.execSQL(sqlStr.toString());
    }

    @Override
    public void delUser(int uid) {
        SQLiteDatabase sd = dbHelper.getWritableDatabase();
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append("DELETE FROM ")
                .append(DbHelper.TABLE_NAME)
                .append(" WHERE ")
                .append("_id=")
                .append(uid);
        sd.execSQL(sqlStr.toString());
    }

    @Override
    public void alterUser(int uid, User newUser) {
        SQLiteDatabase sd = dbHelper.getWritableDatabase();
        StringBuilder sqlStr = new StringBuilder();
        sqlStr.append("UPDATE ")
                .append(DbHelper.TABLE_NAME)
                .append(" SET ")
                .append("userName=")
                .append("'" + newUser.getUserName() + "'")
                .append(",")
                .append("email=")
                .append("'" + newUser.getEmail() + "'")
                .append(",")
                .append("passwd=")
                .append("'" + newUser.getPasswd() + "'")
                .append(" WHERE ")
                .append("_id=")
                .append(uid);
        sd.execSQL(sqlStr.toString());
    }

    @Override
    public User getUser(String email) {
        SQLiteDatabase sd = dbHelper.getWritableDatabase();
        Cursor cursor = sd.query(DbHelper.TABLE_NAME,null,"email='?'",new String[]{email},null,null,null);
        User user = null;
        while(cursor.moveToNext()){
            user = new User();
            user.setUserId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
            user.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            user.setPasswd(cursor.getString(cursor.getColumnIndex("passwd")));
        }
        return user;
    }

    @Override
    public List<User> getUsers() {
        SQLiteDatabase sd = dbHelper.getWritableDatabase();
        Cursor cursor = sd.query(DbHelper.TABLE_NAME,null,null,null,null,null,null);
        List<User> users = new ArrayList<>();
        while(cursor.moveToNext()){
            User user = new User();
            user.setUserId(cursor.getInt(cursor.getColumnIndex("_id")));
            user.setUserName(cursor.getString(cursor.getColumnIndex("userName")));
            user.setEmail(cursor.getString(cursor.getColumnIndex("email")));
            user.setPasswd(cursor.getString(cursor.getColumnIndex("passwd")));
            users.add(user);
        }
        return users;
    }
}
